package acom.test.swing.bb;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;

import acom.test.swing.bb.PeoplePanel.PeopleModelList;

public class PrizePanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JLabel title;
	private JPanel inputPanel;
	private JList plistView;
	private JScrollPane scrollPane;

	private JLabel prizename_lab; // 奖项 名称，一等奖，二等奖
	private JLabel prizenum_lab; // 奖品数量
	private JLabel content_lab; // 奖品内容，礼物
	private JLabel fetchnum_lab; // 抽奖的时候，一次性抽多少人

	private JTextField name_tf;
	private JTextField total_tf;
	private JTextField content_tf;
	private JTextField fetchnum_tf;

	private JButton add_but;
	private JButton del_but;
	private JButton up_but;
	private JButton down_but;
	
	private PrizeModelList mode;

	private List<Prize> prizes;
	private Prize prize;

	public PrizePanel() {
		this.prizes = SystemUtil.getPrizelist();
		this.setLayout(null);
		title = new JLabel("请设置奖项");
		mode = new PrizeModelList();
		plistView = new JList(mode);

		inputPanel = new JPanel();
		GridLayout gird = new GridLayout(2, 4);
		inputPanel.setLayout(gird);

		prizename_lab = new JLabel("奖项名称");
		prizenum_lab = new JLabel("奖项数量");
		content_lab = new JLabel("奖品内容");
		fetchnum_lab = new JLabel("一次抽多少人");

		name_tf = new JTextField();
		total_tf = new JTextField();
		content_tf = new JTextField();
		fetchnum_tf = new JTextField();

		inputPanel.add(prizename_lab);
		inputPanel.add(name_tf);
		inputPanel.add(prizenum_lab);
		inputPanel.add(total_tf);

		inputPanel.add(content_lab);
		inputPanel.add(content_tf);
		inputPanel.add(fetchnum_lab);
		inputPanel.add(fetchnum_tf);

		scrollPane = new JScrollPane(plistView);
		scrollPane.setPreferredSize(new Dimension(350, 300));

		ImageIcon add_icon = SystemUtil.createImageIcon("add_16.png");
		ImageIcon del_icon = SystemUtil.createImageIcon("remove_16.png");
		ImageIcon up_icon = SystemUtil.createImageIcon("up_16.png");
		ImageIcon down_icon = SystemUtil.createImageIcon("down_16.png");

		add_but = new JButton(add_icon);
		del_but = new JButton(del_icon);
		up_but = new JButton(up_icon);
		down_but = new JButton(down_icon);

		setSizeBound();

		this.add(title);
		this.add(inputPanel);
		this.add(scrollPane);
		this.add(add_but);
		this.add(del_but);
		this.add(up_but);
		this.add(down_but);
	}

	private int prPan_x = 20;
	private int prPan_y = 10;

	public void setSizeBound() {
		title.setBounds(prPan_x, prPan_y, 120, 20);
		inputPanel.setBounds(prPan_x, prPan_y + 30, 250, 60);
		add_but.setBounds(prPan_x + 280, prPan_y + 30, 25, 25);
		del_but.setBounds(prPan_x + 280, prPan_y + 60, 25, 25);

		scrollPane.setBounds(prPan_x, prPan_y + 110, 250, 150);
		up_but.setBounds(prPan_x + 280, prPan_y + 110, 25, 25);
		down_but.setBounds(prPan_x + 280, prPan_y + 140, 25, 25);

		add_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// 增加奖项
				AddPrize_Action(e);
			}
		});
		
		del_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				DeletePrize_Action(e);
			}
		});
		up_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				Up_Action(e);
			}
		});
		down_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				Down_Action(e);
			}
		});
	}

	protected void Down_Action(MouseEvent e) {
		int index = plistView.getSelectedIndex();
		System.out.println("当前选中的位置："+index);
		
		if(index<(mode.size()-1)&&index>=0){
			Prize curPrize=prizes.get(index);
			Prize temp =prizes.get(index+1);
			
			prizes.set(index+1, curPrize);
			prizes.set(index, temp);
			
			mode = new PrizeModelList();
			plistView.setModel(mode);
		}
	}

	protected void Up_Action(MouseEvent e) {
		int index = plistView.getSelectedIndex();
		System.out.println("当前选中的位置："+index);
		if(index>0){
			Prize temp =prizes.get(index-1);
			Prize curPrize=prizes.get(index);
			
			prizes.set(index-1, curPrize);
			prizes.set(index, temp);
			
			mode = new PrizeModelList();
			plistView.setModel(mode);
		}
		
	}

	protected void DeletePrize_Action(MouseEvent e) {
		
		int[] index= plistView.getSelectedIndices();
		int endindex=index.length-1;
		for(int i=endindex;i>=0;i--){
			System.out.println("选中的===="+index[i]);
			mode.removeElementAt(index[i]);
			prizes.remove(index[i]);
		}
	}

	protected void AddPrize_Action(MouseEvent e) {
		String pname = name_tf.getText();
		String ptotal = total_tf.getText();
		String pcontent = content_tf.getText();
		String fetchnum = fetchnum_tf.getText();

		if(!checkInputContent( pname, ptotal, pcontent, fetchnum)){
			return;
		}
		
		// 保存 数据
		// System.out.println("pname="+pname+", ptotal="+ptotal+", pcontent="+pcontent);
		prize = new Prize();
		prize.setPname(pname);
		prize.setPcontent(pcontent);
		prize.setTotal(Integer.parseInt(ptotal));
		prize.setFetchnum(Integer.parseInt(fetchnum));
		prize.setIsFinish(false);
		prizes.add(prize);

		// 清除显示的
		name_tf.setText("");
		total_tf.setText("");
		content_tf.setText("");
		fetchnum_tf.setText("");

		mode = new PrizeModelList();
		plistView.setModel(mode);
	}
	
	private boolean checkInputContent(String pname,String ptotal,String pcontent,String fetchnum){
		if (pname == null || pname.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "奖项名称不能为空");
			return false;
		}
		if (pcontent == null || pcontent.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "奖品名称不能为空");
			return false;
		}
		if (ptotal == null || ptotal.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "奖项数量不能为空");
			return false;
		}
		if (!SystemUtil.isInteger(ptotal)) {
			JOptionPane.showMessageDialog(null, "奖项数量不是数字格式");
			return false;
		}
		if (fetchnum == null || fetchnum.trim().isEmpty()) {
			JOptionPane.showMessageDialog(null, "一次抽多少人数,不能为空");
			return false;
		}
		if (!SystemUtil.isInteger(fetchnum)) {
			JOptionPane.showMessageDialog(null, "一次抽奖项人数,不是数字格式");
			return false;
		}
		
		if(Integer.parseInt(fetchnum)>Integer.parseInt(ptotal)){
			JOptionPane.showMessageDialog(null, "一次抽奖的人数 比 总的奖项数量 还多");
			return false;
		}
		
		return true;
	}

	class PrizeModelList extends DefaultListModel// 创建一个继承了DefaultListModel类的类
	{
		private static final long serialVersionUID = 1L;		
		PrizeModelList() {

			for (int i = 0; i < prizes.size(); i++) {
				Prize priz = prizes.get(i);
				addElement((i + 1) + "." + priz.getPname() + "  "
						+ priz.getTotal());
			}
		}
	}
}
